<template>
  <div class="selector-menu">
    <template v-if="list.length > 0">
      <div class="menu-item" v-for="(item, index) of list" :key="index" @click="setItemValue(item)">
        {{ item.label }}
      </div>
    </template>
    <NoDataTip v-else />
  </div>
</template>

<script>
import NoDataTip from "./NoDataTip"

export default {
  name: "SelectorMenu",
  components: {
    NoDataTip,
  },
  data() {
    return {
      searchData: [],
    }
  },
  props: {
    searchValue: String,
    data: {
      type: Array,
      default: [
        {
          id: 1,
          value: "1",
          text: "选项1",
        },
        {
          id: 2,
          value: "2",
          text: "选项2",
        },
        {
          id: 3,
          value: "3",
          text: "选项3",
        },
      ],
    },
    isReload: Boolean,
  },
  methods: {
    setItemValue(item) {
      this.$emit("setItemValue", item)
    },
    filterData(value) {
      this.searchData = this.data.filter((item) => {
        return item.label.toLowerCase().includes(value.toLowerCase())
      })
    },
  },
  watch: {
    searchValue(value) {
      if (this.isReload) {
        this.$emit("updateSearch")
      } else {
        this.filterData(value)
      }
    },
  },
  computed: {
    list() {
      return this.isReload ? this.data : this.searchData
    },
  },
  mounted() {
    this.searchData = this.data
  },
}
</script>

<style lang="scss" scoped>
.selector-menu {
  display: none;
  position: absolute;
  left: 0;
  top: 48px;
  width: 100%;
  max-height: 100px;
  overflow-y: scroll;
  background-color: #fff;
  box-shadow: 0 0 10px #ddd;
  border-radius: 5px;
  // cursor: pointer;

  .menu-item {
    padding: 10px 5px;
    font-size: 14px;
    color: #666;
    margin: 10px 0;
    transition: background-color 0.2s linear;

    &:hover {
      background-color: #ededed;
    }
  }
}
</style>
